# 第一阶段：构建 Go 应用
FROM golang:1.23.4-alpine AS builder
# 接收构建参数
ARG ENV=uat
ENV GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
# 设置工作目录
WORKDIR /workspace
# 复制源代码
COPY . .

# 处理公共依赖
WORKDIR /workspace/common
RUN go mod download


# 构建主应用
WORKDIR /workspace/gateway
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -o app -ldflags="-X 'main.env=${ENV}'"


# 第二阶段：生成最小运行时镜像
FROM alpine:latest


# 设置工作目录
WORKDIR /app
COPY --from=builder /workspace/gateway/etc ./etc
COPY --from=builder /workspace/gateway/app .

# 暴露端口（根据实际需要修改）
EXPOSE 8888

# 启动应用
CMD ["./app"]
